package de.fhhannover.inform.itteam.snmpnetviz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.snmp4j.smi.OID;
import org.snmp4j.util.TableEvent;

import de.fhhannover.inform.itteam.snmpnetviz.snmp.SnmpAdapter;

public class IpMacInterfaceTable {
	private HostEntry mHost;
	private String[][] mSwitchConnections;

	private ArrayList<HostEntry> ports = new ArrayList<HostEntry>();
	
	private IpToMac mIpMacTable;
	
	public IpMacInterfaceTable(HostEntry host, IpToMac ipMacTable, String[][] switchconnections) {
		mHost = host;
		mIpMacTable = ipMacTable;
		mSwitchConnections = switchconnections;
		updateContent();
	}	
	
	public HostEntry getHostForIp(String ip) {
		Iterator<HostEntry> it = ports.iterator();
		
		while(it.hasNext()) {
			HostEntry current = it.next();
			if (current.getIpAddress()!=null && current.getIpAddress().equals(ip)) return current;
		}
		
		return null;
	}
	
	public Iterator<HostEntry> getPortEntrys() {
		return ports.iterator();
	}
	
	public void updateContent() {
		SnmpAdapter snmp = new SnmpAdapter("udp:"+mHost.getIpAddress()+"/161", "public");
		
		// get the VLAN Table from switch
		OID testTable = new OID("1.3.6.1.4.1.9.9.46.1.3.1.1.2.1");
		List<TableEvent> testEntrys = snmp.getTable(testTable);
		Iterator<TableEvent> testIT = testEntrys.iterator();
		// for each VLAN
		while (testIT.hasNext()) {
			TableEvent testEntry = testIT.next();
			
			// extract the VLAN index
			int vlanIndex = 1;
			if (testEntry!=null && testEntry.getIndex()!=null) vlanIndex = new Integer(testEntry.getIndex().toString()).intValue();
			
			// get the bridge port index table for the VLAN
			SnmpAdapter vlanSnmp = new SnmpAdapter("udp:"+mHost.getIpAddress()+"/161", "public@"+vlanIndex);
			OID vlanBridgeOid = new OID("1.3.6.1.2.1.17.4.3.1.2");
			List<TableEvent> vlanBridgeEntrys = vlanSnmp.getTable(vlanBridgeOid);
			Iterator<TableEvent> vlanBridgeIt = vlanBridgeEntrys.iterator();

			// get the interface index for the bridge port table
			HashMap<Integer,String> bridgePortToInterfaceTable = HelperFunctions.getHashmapFromOidTable(vlanSnmp.getTable(new OID("1.3.6.1.2.1.17.1.4.1.2")));
			
			// get the description of the interface port table
			HashMap<Integer,String> interfacePortDescriptionTable = HelperFunctions.getHashmapFromOidTable(vlanSnmp.getTable(new OID("1.3.6.1.2.1.31.1.1.1.1")));
			
			// for each VLAN MAC entry
			while (vlanBridgeIt.hasNext()) {
				TableEvent next = vlanBridgeIt.next();
				if (next.getIndex()!=null) {
					String[] split2 = next.getIndex().toString().split("[.]");
					String macOidString = split2[split2.length-6]+"."+split2[split2.length-5]+"."+split2[split2.length-4]+"."+split2[split2.length-3]+"."+split2[split2.length-2]+"."+split2[split2.length-1];
					
					//generate the MAC String
					String mac = HelperFunctions.getMacFromOidString(macOidString);
					
					int currentVlanBridgePort = new Integer(next.getColumns()[0].toValueString()).intValue();
					
					// search the interface port to the bridge port
					String tempString = bridgePortToInterfaceTable.get(new Integer(currentVlanBridgePort).intValue());
					int interfaceIndex;
					if (tempString==null) {
						interfaceIndex=0;
					}
					else {
						interfaceIndex = new Integer(tempString).intValue();
					}
					
					//get the description of the mac
					String portDesc = interfacePortDescriptionTable.get(new Integer(interfaceIndex));
					
					// get the ip with mac over the ip to mac table
					String ip = mIpMacTable.getIpForMac(mac);
					
					// determaine if the host is a switch
					boolean isSwitch=false;
					for (String[] curSwitch : mSwitchConnections) {
						if (curSwitch[1].equals(ip)) isSwitch=true;
					}
					
					// adding the HostEntry to the Collection
					if (ip!=null) ports.add(new HostEntry(mHost, vlanIndex, ip, mac, currentVlanBridgePort, interfaceIndex, portDesc, isSwitch));
				}				
			}
		}
	}

	public String toString() {
		String output ="";
		
		output += "SwitchIp: "+mHost.getIpAddress()+"\n";
		
		Iterator<HostEntry> it = ports.iterator();
		while(it.hasNext()) {
			output += it.next().toString() + "\n";
		}
		
		return output;
	}
}
